package com.liyuhan.shopping.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.liyuhan.shopping.dto.DataResponse;
import com.liyuhan.shopping.entity.ShoppingCart;
import com.liyuhan.shopping.entity.User;
import com.liyuhan.shopping.mapper.ShoppingCarMapper;
import com.liyuhan.shopping.mapper.UserMapper;
import com.liyuhan.shopping.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private UserMapper userMapper;

    private Map<String, String> map = new HashMap<>();

    public DataResponse add(JSONObject request){
        String uname = request.get("uname").toString();
        int gid = Integer.parseInt(request.get("gid").toString());
        int gcount = Integer.parseInt(request.get("count").toString());
        User user = userMapper.selectByUsername(uname);
        ShoppingCart shoppingCart = new ShoppingCart(user.getUid(), gid, gcount);
        shoppingCarMapper.insert(shoppingCart);
        return new DataResponse("200", map);
    }
}
